package evocarcassonne.core.board.locations;

import evocarcassonne.core.elements.Element;
import evocarcassonne.core.utils.Feature;
import evocarcassonne.core.utils.ZoneType;

import java.util.HashSet;
import java.util.Set;

public class Zone {

    private ZoneType type = ZoneType.MEADOW;
    Set<Zone> links = new HashSet();
    private Element element;
    public int card;
    private boolean deadEnd = true;
    private int touch = -1;
    private boolean placePawn = false;
    Set<Integer> connections;
    Set<Feature> features;

    public void resetLinks() {
        links = new HashSet();

        if (type == ZoneType.ROAD) {

            // if it's a road, restore default deadEnd
            setDeadEnd(true);

            // if it's a road, remove touch
            disableTouch();
        }
    }

    public void setConnections(Integer... conns) {

        connections = new HashSet();

        for (int i = 0; i < conns.length; i++) {
            connections.add(conns[i]);
        }
    }

    public void setFeatures(Feature[] feats) {
        features = new HashSet();

        for (int i = 0; i < feats.length; i++) {
            features.add(feats[i]);
        }
    }

    public void addLink(Zone zone) {
        links.add(zone);

        if (type == ZoneType.ROAD) {

            // if it's a road, remove deadEnd
            setDeadEnd(false);
            zone.setDeadEnd(false);

            // if it's a road, remove touch
            disableTouch();
            zone.disableTouch();
        }
    }

    public Zone(int card) {
        this.card = card;
    }

    public Element getElement() {
        return element;
    }

    public void setElement(Element element) {
        this.element = element;

        propagateElement();
    }

    public Set<Zone> getLinks() {
        return links;
    }

    private void propagateElement() {

        for (Zone zone : links) {
            zone.element = element;
        }
    }

    public void setType(ZoneType i) {
        type = i;
    }

    public ZoneType getType() {
        return type;
    }

    public boolean isDeadEnd() {
        return deadEnd;
    }

    public boolean canPlacePawn() {
        return placePawn;
    }

    public void enablePawn() {
        this.placePawn = true;
    }

    @Override
    public String toString() {
        return "Zone{"
                + "card=" + card
                + '}';
    }

    public void setDeadEnd(boolean b) {
        deadEnd = b;
    }

    public void disableTouch() {
        touch = -1;
    }

    public void setTouch(int zone) {
        touch = zone;
    }

    public int getTouch() {
        return touch;
    }
}
